package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.SystemDictionaryItem;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.p2p.base.service.ISystemDictionaryItemService;
import cn.wolfcode.p2p.base.service.ISystemDictionaryService;
import cn.wolfcode.p2p.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/systemDictionaryItem_list")
    public String systemDictioanryItemPage(@ModelAttribute("qo") SystemDictionaryItemQueryObject qo, Model model){
        //systemDictionaryGroups
        model.addAttribute("systemDictionaryGroups", systemDictionaryService.selectAll());
        //pageInfo
        model.addAttribute("pageInfo", systemDictionaryItemService.queryPage(qo));
        return "systemdic/systemDictionaryItem_list";
    }


    @RequestMapping("/systemDictionaryItem_update")
    @ResponseBody
    public JSONResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
        JSONResult result = null;
        try {
            if (systemDictionaryItem.getParentId() == null) {
                return new JSONResult(false,"请选择字典分类");
            }
            systemDictionaryItemService.saveOrUpdate(systemDictionaryItem);
            result = new JSONResult("");
        } catch (DisplayableException ex) {
            //异常需要在前台显示
            ex.printStackTrace();
            result = new JSONResult(false, ex.getMessage());
        } catch (Exception e){
            //异常记录到数据库或日志文件中
            e.printStackTrace();
            result = new JSONResult(false, "系统异常,请联系管理员");
        }
        return result;
    }
}
